package com.distributedDataBase.mapper;

import com.distributedDataBase.pojo.User;
import com.distributedDataBase.pojo.UserVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {

    @Select("select * from tbl_user")
    List<User> getAllUsers();

    // 一个参数，命名随意
    @Select("select * from tbl_user where email = #{value}")
    User findUserByEmail(String mail);

    // 多个参数，使用@Param
    @Select("select * from tbl_user where username = #{uname} and password = #{pwd}")
    User findUserByNameAndPwd(@Param("uname") String admin, @Param("pwd") String number);

    // 传入自定义类型对象，命名和自定义类中属性名一致
    @Select("select * from tbl_user where username = #{username} and password = #{password}")
    User getUserByNameAndPwd(User user);

    @Insert("insert into tbl_user values (#{id}, #{username}, #{password}, #{email}, #{power})")
    int addUser(User user);

    @Select("select case power\n" +
            "when 0 then '管理员'\n" +
            "when 1 then '普通人员'\n" +
            "else '其他'\n" +
            "end 'role',power_count\n" +
            "from (select power,count(id) power_count from tbl_user group by power) core_;")
    List<UserVo> powerCount();

    // xml开发方式
    User getUserById(int id);

    // 动态语句查询
    List<User> getUserByNameAndPower(@Param("uname") String admin, @Param("power") String number);

    // 分页查询
    List<User> listUsers(@Param("username") String username,@Param("pageNow") Integer pageNow,@Param("pageSize") Integer pageSize);

    // 查询模糊查询的总条数
    Integer countUsers(String username);

    //为了给分页插件使用的
    @Select("select * from tbl_user where username like concat('%',#{value},'%')")
    List<User> listByUsername(String username);
}
